package com.nervenets.template.config;

import com.nervenets.general.enumeration.ClientType;
import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.model.MenuRole;
import com.nervenets.general.service.RedisService;
import com.nervenets.general.utils.AESUtils;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.template.enumeration.Gender;
import com.nervenets.template.hibernate.dao.RoleGroupDao;
import com.nervenets.template.hibernate.domain.common.GlobalRule;
import com.nervenets.template.hibernate.domain.common.GlobalRuleSetting;
import com.nervenets.template.hibernate.domain.user.Manager;
import com.nervenets.template.hibernate.domain.user.RoleGroup;
import com.nervenets.template.service.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Order(10)
@Configuration
public class DataInit implements InitializingBean {
    public static final String INIT_ACCOUNT = "13800138000";
    public static final String INIT_PASSWORD = "123456";
    public static final String INIT_ROLE_GROUP_NAME = "超级管理员";

    @Resource
    private RoleGroupDao roleGroupDao;
    @Resource
    private ManagerService managerService;
    @Resource
    private RedisService redisService;
    @Resource
    private Environment environment;
    @Resource
    private DataOfUserService dataOfUserService;
    @Resource
    private DataOfMoneyService dataOfMoneyService;
    @Resource
    private DataOfGoldService dataOfGoldService;
    @Resource
    private GlobalRuleService globalRuleService;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (Arrays.toString(environment.getActiveProfiles()).contains("dev")) {
            redisService.clearRedis();
        }

        Manager manager = managerService.findByMobile(INIT_ACCOUNT);
        if (null == manager) {
            RoleGroup roleGroup = RoleGroup.builder()
                    .clientType(ClientType.administrators)
                    .name(INIT_ROLE_GROUP_NAME)
                    .detail("超级管理员权限组")
                    .roles(JwtUtils.getAllMenuRoleIds())
                    .interfaces(JwtUtils.getAllInterfaceRoleIds())
                    .queue(999)
                    .updateTime(JodaUtils.getTimestamp())
                    .build();

            roleGroupDao.save(roleGroup);

            List<RoleGroup> roleGroups = new ArrayList<>();
            roleGroups.add(roleGroup);

            manager = Manager.builder()
                    .mobile(INIT_ACCOUNT)
                    .password(AESUtils.encryptString(INIT_PASSWORD))
                    .name("管理员")
                    .roleGroups(roleGroups)
                    .gender(Gender.female)
                    .build();

            managerService.save(manager);
        } else {
            RoleGroup topRoleGroup = roleGroupDao.findTop1ByOrderById();
            if (null != topRoleGroup) {
                topRoleGroup.setRoles(JwtUtils.getAllMenuRoleIds());
                topRoleGroup.setInterfaces(JwtUtils.getAllInterfaceRoleIds());
                topRoleGroup.setUpdateTime(JodaUtils.getTimestamp());
                roleGroupDao.save(topRoleGroup);
            }
        }

        if (null == globalRuleService.getGlobalRule()) {
            GlobalRule globalRule = GlobalRule.builder()
                    .name("默认全局设置")
                    .setting(new GlobalRuleSetting())
                    .enable(true)
                    .build();
            globalRule.setCreateBy(manager.getId());
            globalRuleService.save(globalRule);
        }

        this.dataCheck();
    }

    private void dataCheck() {
        System.out.println("检查统计基础数据是否生成...");
        final long tomorrow = JodaUtils.getEndOfToday() + 1;
        final long today = JodaUtils.getStartOfToday();

        dataOfUserService.checkAndGenerateData(today);
        dataOfUserService.checkAndGenerateData(tomorrow);

        dataOfMoneyService.checkAndGenerateData(today);
        dataOfMoneyService.checkAndGenerateData(tomorrow);

        dataOfGoldService.checkAndGenerateData(today);
        dataOfGoldService.checkAndGenerateData(tomorrow);
    }
}
